using System;
using AndyPike.Unfuddle.Commands.Tickets;
using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Tickets;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using System.Collections.Generic;

namespace AndyPike.Unfuddle.Tests.Unit.Commands.Tickets
{
    [TestFixture]
    public class When_querying_unfuddle_for_all_tickets_for_a_project
    {
        private MockRepository mockery;
        private IUnfuddleService service;
        private FindAllTicketsForProject command;

        [SetUp]
        public void SetUp()
        {
            mockery = new MockRepository();
            service = mockery.DynamicMock<IUnfuddleService>();
            command = new FindAllTicketsForProject(123);
        }

        [Test]
        public void Should_leverage_the_unfuddle_service()
        {
            const string url = "http://www.domain.com";
            List<Ticket> actualTickets;
            var ticketsCollection = new TicketsCollection();
            ticketsCollection.Tickets.Add(new Ticket());

            using (mockery.Record())
            {
                Expect.Call(service.BuildUrl(null)).IgnoreArguments().Return(url);
                Expect.Call(service.InvokeRequest<TicketsCollection>(url)).Return(ticketsCollection);
            }

            using (mockery.Playback())
            {
                actualTickets = command.Execute(service);
            }

            Assert.That(actualTickets, Is.EquivalentTo(ticketsCollection.Tickets));
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_a_null_service_is_passed_in()
        {
            command.Execute(null);
        }
    }
}